/**
 * Created by zhaocong on 2016/6/4.
 */


/**
 * 根据频幕改变页面尺寸
 * @param size 基础大小
 */
(function (size) {
  var docEl = document.documentElement,
    resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
    recalc = function() {
      docEl.style.fontSize = 20*(docEl.clientWidth/size)+'px';
    };
  window.addEventListener(resizeEvt, recalc, false);
  document.addEventListener('DOMContentLoaded', recalc, false);
})(375);


/**
 * 选择卖场
 * @type {{storebox: (*|jQuery|HTMLElement), storecrl: (*|jQuery|HTMLElement), show: Function, hideen: Function, layer: Function, moremarket: Function, init: Function}}
 */
var stores = {
  storebox: $('.stores'),
  storecrl: $('#stores'),
  show: function () {
    stores.storebox.show();
    $('body,html').addClass('ovfHiden');
  },
  hideen: function () {
    stores.storebox.hide();
    $('body,html').removeClass('ovfHiden');
  },
  layer: function () {
    var bg = $(this).children('img').attr('src'),
        val = $(this).children('span').text();
    stores.storecrl.val(val).addClass('bg-item').css({'background-image':'url('+bg+')'});
    stores.hideen();
  },
  moremarket: function () {
    var _ul = $('.st-ul'),
        top = _ul.scrollTop(),
        // load
        _html = '<li class="layer"><img class="st-img" src="img/data/walmart.png" alt="supper"/><span class="st-val">--加载--</span></li>';

    $('.moremarket-list').html('').html(_html);
    _ul.scrollTop(top + 20);
  },
  init: function () {
    var _this = this;
    $(_this.storecrl).on('tap',_this.show);
    $('.stores-mask').on('tap',_this.hideen);
    $('.st-ul').on('tap','.layer',_this.layer);
    $('#moremarket').on('tap',_this.moremarket);
  }
};


/**
 * 表单验证提交
 */
var form = {
  url:'',
  img: 0,
  sec: 0,
  event: function () {
    $('#upimg').on('tap',form.wxUpload);
    $('#goon').on('tap',form.case);
    $('.radiobox').on('tap',form.radiosec);
  },
  case: function () {
    var _sto = $('#stores').val(),
        _time = $('#datetime').val(),
        _num = $('#num').val(),
        data = {
          store: _sto,
          time: _time,
          num: _num
        };
    if(_sto && _time && _num){
      form.ajax(data);
    }else{
      $('#load-info').html('请将信息填写完整').show();
    }
  },
  radiosec:function () {
    var _this = $(this),
        $upimg = $('#upimg'),
        value = _this.attr('data-val');
    _this.addClass('action').siblings().removeClass('action');
    _this.siblings('.radio-val').val(value);
    (value === '0')?$upimg.html('整张上传'):$upimg.html('分段上传');
    form.sec = value;
  },
  ajax: function (data) {
    $.ajax({
      url: form.url,
      type: 'get',
      data: data,
      beforeSend: function(){
        $('#load-info').html('<i class="loading"></i> 小票上传中').show();
        $('#upimg').off('tap');
      },
      success: function(){
        $('#load-info').html('上传成功!').show();
        //window.location.href = '';
      },
      error: function(){
        $('#load-info').html('上传失败!').addClass('red').show();
        form.event();
      }
    });
  },
  wxUpload: function () {
    if(form.sec == 0){
      alert('ol');
      $('#upimg').hide();
    }else{
      alert('ol002');
    }
    ++form.img;
    $('#goon').show();
  },
  init: function (url) {
    this.url = url;
    this.event();
  }
};

/**
 * 判断当前页面的状态
 */
var dataPage = function (pren) {
  var page = $('body').data('page'),
    _tit = $('.time-tit'),
    _line = $('.linebox'),
    _img = $('.status-tit'),
    _info = $('.info-content'),
    _btng = $('.btn-group'),
    _pint = $('.point');
  switch (page){
    case 1:
      break;
    case 2:
      _tit.html('审核进行中');
      _pint.attr('data-size',pren+'%');
      _line.addClass('t1');
      addPren();
      break;
    case 3:
      _img.attr('src','img/success-page.png');
      _tit.html('审核通过');
      _line.addClass('t2');
      _pint.attr('data-size','100%');
      _info.html('<p>您的小票已审核通过。</p>'+
        '<p>10元微信红包已发送到您账户中，请注意查收。如有问题请联系客服。</p>' +
        '<p>感谢您的参与！</p>');
      break;
    case 4:
      _img.attr('src','img/erro.png');
      _tit.html('审核失败');
      _line.addClass('t2');
      _pint.attr('data-size','100%');
      _info.html('<p>您的小票审核未通过。</p>'+
        '<p>可能原因：小票信息不清晰或不满足活动规则，请重新上传并仔细阅读活动规则。</p>' +
        '<p>感谢您的参与！</p>');
      _btng.html('<a class="btn-default btn-gro" href="tel:4006666225">联系客服</a><a class="btn-default btn-gro" href="javascript:;">重新上传</a>');
      break;
  }

  function addPren() {
    $('.linebox .before').css('width',pren+'%');
    $('.linebox .point').css('left',pren+'%');
  }
};


/**
 * 小于ip4尺寸时要执行的任务
 * @param task 函数
 */
var mediaIp4 = function (task) {
  window.onload = function () {
    var w = $(window).width();
    var h = $(window).height();
    if(h/w <= 430/320){
      task();
    }
  }
}
